package com.example.back.service;

import com.example.back.entity.Notice;
import com.example.back.entity.Relations;
import com.example.back.mapper.NoticeMapper;
import com.example.back.mapper.RelationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


@Service
public class RelationService {
    @Autowired
    private RelationMapper relationMapper;
    @Autowired
    private NoticeMapper noticeMapper;

    public int addRelation(Relations relations){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int result = relationMapper.addRelation(relations);// 添加关系
        if (result>0){
            Notice notice = new Notice();// 创建通知
            notice.setUserId(relations.getUserId());
            notice.setTriggerUserId(relations.getFanId());
            notice.setTime(sdf.format(new Date()));
            notice.setText("用户 " + relations.getFanId() + " 关注了您");
            notice.setType("follow");
            notice.setIsRead(0);
            noticeMapper.addNotice(notice);
        }
        return result;
    }
    public ArrayList<Relations> queryUserRelation(Relations relations){
        return relationMapper.queryUserRelation(relations);
    }

    public int deleteRelation(Integer id){
        return relationMapper.deleteRelation(id);
    }

    public int deleteRelationByUserAndFan(Integer fanId, Integer userId){
        return relationMapper.deleteRelationByUserAndFan(fanId, userId);
    }
    public Relations getRelationByUserAndFan(Integer fanId, Integer userId){
        return relationMapper.getRelationByUserAndFan(fanId, userId);
    }
}